// import store from '@/store'
// import { isNull } from '@/utils/utils'

// 判断是否为空
function isNull(value) {
	if (value === 0 || value === '0') {
    // 若不去单独处理0,0会被默认判定为空，所以这里特殊处理0为非空
		return false
	} else if (value === '' || value === 'undefined' || value === undefined || value === null || value === 'null') {
    return true
  } else {
    if (typeof value == 'object') {
      // 这里单独判断空数组和空对象
      if (value.length <= 0 || Object.keys(value).length <= 0) {
        return true
      } else {
        return false
      }
    } else {
			return false
		}
	}
}

function getHospitalCode() {
  return localStorage.getItem('jjyy-hospitalCode')
}
// 设置院区级缓存
export function setLocalStorage(key, data, hospitalCode = getHospitalCode()) {
  // console.log('hospitalCode', hospitalCode)
  localStorage.setItem(`${key}-${hospitalCode}`, data)
  if (!isNull(localStorage.getItem(`${key}`))) {
    localStorage.setItem(`${key}`, data)
  }
}
// 获取院区级缓存
export function getLocalStorage(key, hospitalCode = getHospitalCode()) {
  // // console.log('into getLocalStorage key:', key)
  // if (key == 'YJYY-USERMENU') {
  //   let ls_value = localStorage.getItem(`${key}-${hospitalCode}`)
  //   // console.log('ls_value', ls_value)
  // }
  // if (key == 'patientSourceToqueryWay') {
  //   // console.log('patientSourceToqueryWay')
  //   let ls_value = localStorage.getItem(`${key}-${hospitalCode}`)
  //   // console.log('ls_value', ls_value)
  // }
  // setLocalStorage
  // 院区缓存没有，历史缓存有，先取历史缓存，不然更新到医院，缓存都要重新设置，会被说的
  if (isNull(localStorage.getItem(`${key}-${hospitalCode}`)) && !isNull(localStorage.getItem(`${key}`)) && (key == 'CHECK_FORM_DEFAULT' || key == 'localSetMemory' || key == 'defaultReadCard' || key == 'jjyy-isSetStorageAreaName' || key == 'jjyy-isShowTwoList' || key == 'UserPermision' || key == 'VirtualRoomKey' || key == 'YJYY-DEFAULTROOM' || key == 'hospitalOptions')) {
    if (key == 'VirtualRoomKey') {
      return localStorage.getItem(`${key}`) || -1
    }
    // if (key == 'hospitalOptions') {
    //   console.log('into-key', key, localStorage.getItem(`${key}`))
    // }
    return localStorage.getItem(`${key}`) || ''
  }
  if (key == 'VirtualRoomKey') {
    return localStorage.getItem(`${key}-${hospitalCode}`) || -1
  }
  return localStorage.getItem(`${key}-${hospitalCode}`) || ''
}
// 删除院区级缓存
export function removeLocalStorage(key, data) {
  const hospitalCode = getHospitalCode()
  if (!isNull(localStorage.getItem(`${key}-${hospitalCode}`))) {
    localStorage.removeItem(`${key}-${hospitalCode}`)
  }
  if (!isNull(localStorage.getItem(`${key}`))) {
    localStorage.removeItem(`${key}`)
  }
}
